<template>
	<view class="page" >
		
		<view class="hidden" style="width: 100%;height: 13%;position: absolute;" @click="back"></view>
		
		<view class="account-block">
			<view class="account-title">
				<text>切换账号</text>
			</view>
			<view class="account-details">
				<scroll-view class="account-list" scroll-x="true" show-scrollbar="false">
					<image 
					v-for="(user,index) in userArray" :key = index 
					@click="changUser(user)"
					:src="user.headPic" class="account-headPic"></image>
				</scroll-view>
				<view class="add-account" @click="qqLogin">
					<label class="iconfont iconjiahao"></label>
				</view>
				<view class="logout" @click="qqLoginOut">
					<label class="iconfont icontuichu"></label>
				</view>
			</view>
			
		</view>
		
		<view class="status-now">
			<view class="status-now-title">
				我的在线状态
			</view>
			<view class="status-now-details">
				<label class="iconfont icondian"></label>
				<text class="status-text">在线</text>
				<label class="iconfont icon113fangxiang_xiangyou"></label>
			</view>
		</view>
		
		<view class="status-classify">
			<view class="smart-status">
				<label class="iconfont iconjiqiren"></label>
				<text class="smart-status-text">智能状态</text>
			</view>
			<view class="user-defined">
				<label class="iconfont iconshouji"></label>
				<text class="user-defined-text">自定义设备名</text>
			</view>
		</view>
		
		<view class="status-list">
			<view class="status-single" v-for="i in 12" :key = i >
				<label class="status-single-icon iconfont icondian"></label>
				<text class="status-single-text">在线</text>
			</view>
		</view>
		
		<view class="hidden" style="width: 100%;height: 18%;position: absolute;bottom: 0;" @click="back"></view>
		
	</view>
	
</template>

<script>
	
	var _self
	import user from "@/api/user.js"
	
	export default {
		data() {
			return {
				faceImage: '@/static/qq.jpeg',
				nickName: '',
				isLogin: false,
				userArray: [],
				user:{} // 用户数据
			}
		},
		onLoad() {
			
			_self = this
			
			this.Init()
			
			var userinfo = uni.getStorageSync("userInfo")
			if(userinfo == ""){
				// 跳转登录页面
				uni.redirectTo({
					url:'../addAndRegister/addAndRegister',
					animationType: 'pop-in',
					animationDuration: 200
				})
			}else{
				_self.user = userinfo
			}
			
		},
		methods: {
			// 切换用户
			changUser(user){
				uni.setStorageSync("userInfo",user);
				var userList = uni.getStorageSync("userList")
				for (var i = 0; i < userList.length; i++) {
					if(userList[i].id === user.id){
						// debugger
						// 更新 提到第一个
						userList.splice(i,1)
						userList.unshift(user)
					}
				}
				uni.setStorageSync("userList",userList)
				// 跳转
				console.log('跳转了')
				uni.switchTab({
					url:'/pages/message/message'
				})
			},
			back(){
				uni.navigateBack({
					delta:1
				})
			},
			long(){
				console.log('长按了')
			},
			Init() {
				var userList = uni.getStorageSync("userList")
				// console.log(JSON.stringify(userList))
				if(userList == null){
					// 没有登录 去登录
					_self.qqLogin()
				}else{
					this.userArray = userList
				}
			},
			qqLogin() {
				
				uni.navigateTo({
					url:'../addAndRegister/addAndRegister',
					animationType: 'slide-in-bottom',
					animationDuration: 400
				})
				
				// uni.login({
				// 	provider: 'qq',
				// 	success(loginRes) {
						
				// 		// 获取用户信息
				// 		uni.getUserInfo({
				// 			provider:"qq",
				// 			success(info) {
				// 				// console.log(JSON.stringify(info))
				// 				var userInfo = info.userInfo
								
				// 				_self.faceImage = userInfo.figureurl_qq
				// 				_self.nickName = userInfo.nickName
				// 				_self.isLogin = true
				// 				uni.setStorageSync("userInfo",userInfo)
								
				// 				var userList = _self.getUserList()
				// 				if(userList == null){
				// 					userList = []
				// 					userList.push(userInfo)
				// 					uni.setStorageSync("userList",userList)
				// 					_self.userArray = userList
				// 				}else{
				// 					// 判断是否包含
				// 					if(userList.indexOf(userInfo) == -1){
				// 						// 不包含
				// 						userList.push(userInfo)
				// 						uni.setStorageSync("userList",userList)
				// 						_self.userArray = userList
				// 					}
				// 				}
								
								
				// 			}
				// 		})
						
				// 	}
				// })
				
			},
			
			qqLoginOut(){
				user.logout()
			}
		}
	}
</script>

<style>
	@import url("account-management.css");
</style>
